/**
 * 详情对话框
 */
var FlowConfigInfoDlg = {
    data: {
        configId: "",
        commentTags: "",
        companyPhone: "",
        serviceTime: "",
        platform: "",
        couponTop: "",
        couponBottom: "",
        couponBgColor: "",
        platformCover:"",
        orderCount: "",
        helpCount: "",
        userAgreement: "",
        agreementDetail: "",
        isOpenPhoneLogin: "",
        qcodeSlogan:""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects','colorpicker','inputTags','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var colorpicker = layui.colorpicker;
    var laydate = layui.laydate;
    var inputTags = layui.inputTags;


    colorpicker.render({
        elem: '#couponBgColorPicker'
        ,change: function(color){
            console.log(color)
            $("#couponBgColor").val(color);
        }
    });


    laydate.render({
        elem: '#serviceTime'
        , range: true, //或 range: '~' 来自定义分割字符
        type:"time"
    });





    //普通图片上传
    var upload = layui.upload;
    upload.render({
        elem: '#couponTopBtn'
        ,url: Feng.ctxPath + '/system/upload' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            $("#couponTop").val("/upload/"+res.data.fileName);
            Feng.success(res.message);
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });


    upload.render({
        elem: '#couponBottomBtn'
        ,url: Feng.ctxPath + '/system/upload' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview2').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            $("#couponBottom").val("/upload/"+res.data.fileName);
            Feng.success(res.message);
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });

    upload.render({
        elem: '#platformCoverBtn'
        ,url: Feng.ctxPath + '/system/upload' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            $("#platformCover").val("/upload/"+res.data.fileName);
            Feng.success(res.message);
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });


    //实例化编辑器
    var ue = UE.getEditor('agreementDetail', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/ueditor/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/flowConfig/detail");
    var result = ajax.start();
    form.val('flowConfigForm', result.data);
    $('#filePreview1').attr('src', result.data.couponTop); //图片链接（base64）
    $('#filePreview2').attr('src', result.data.couponBottom); //图片链接（base64）
    $('#filePreview').attr('src', result.data.platformCover); //图片链接（base64）
    var tags = inputTags.render({
        elem:'#commentTags',
        content:result.data.commentTags == '' ? [] : result.data.commentTags.split(","),//是否开启获取所有数据的按钮
        done: function(value){ //回车后的回调
            console.log(value);
            console.log(tags);
        },//默认标签
        aldaBtn: true,//是否开启获取所有数据的按钮
    });
    $("#commentTags").val("");
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var tagstr = "";
        if(tags.config.content && tags.config.content.length > 0){
            for (var i = 0;i<tags.config.content.length;i++){
                if(i == 0){
                    tagstr += tags.config.content[i];
                }else {
                    tagstr += ","+tags.config.content[i];
                }
            }
        }
        data.field.commentTags = tagstr;
        data.field.agreementDetail = encodeURIComponent(data.field.agreementDetail);
        var ajax = new $ax(Feng.ctxPath + "/flowConfig/editItem", function (data) {
            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

});